﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;

public class BuildAPK : MonoBehaviour
{

    private static string m_APkName = "BuildApk";
    public static string m_AndroidPath = Application.dataPath + "/../BuildTarget/Android/";
    public static string m_IOSPath = Application.dataPath + "/../BuildTarget/IOS/";
    public static string m_WindowsPath = Application.dataPath + "/../BuildTarget/Windows/";


    [MenuItem("Tools/生成发布标准包")]
    public static void Build()
    {
        //打AB包
        BundleEditor.BuildAB();
        //生成可执行程序
        string abPath = Application.dataPath + "/../AssetBundle/" + EditorUserBuildSettings.activeBuildTarget+"/";
        ///拷贝文件到streamingAssetsPath文件夹下
        Copy(abPath,Application.streamingAssetsPath);
        string savepath = "";
        if (EditorUserBuildSettings.activeBuildTarget==BuildTarget.Android)
        {
            savepath = m_AndroidPath + m_APkName + EditorUserBuildSettings.activeBuildTarget + string.Format("_{0:yyyy_MM_dd_HH_mm}",DateTime.Now) + ".apk";
        }
       else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
        {
            //打出xcode工程
            savepath = m_IOSPath + m_APkName + EditorUserBuildSettings.activeBuildTarget + string.Format("_{0:yyyy_MM_dd_HH_mm}", DateTime.Now );
        }
        else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.StandaloneWindows|| EditorUserBuildSettings.activeBuildTarget ==BuildTarget.StandaloneWindows64)
        {
            savepath = m_WindowsPath + m_APkName + EditorUserBuildSettings.activeBuildTarget + string.Format("_{0:yyyy_MM_dd_HH_mm}/{1}.exe", DateTime.Now,m_APkName);
        }
      ///打包
      BuildPipeline.BuildPlayer(FindEnableEitorScenes(), savepath, EditorUserBuildSettings.activeBuildTarget, BuildOptions.None);

        DeleteDir(Application.streamingAssetsPath);
    }


    private static string[] FindEnableEitorScenes()
    {
        List<string> editorscenes = new List<string>();
        ///获取编辑器下面设置好的所以场景
        foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
        {
            if (!scene.enabled) continue;
            editorscenes.Add(scene.path);   
        }

        return editorscenes.ToArray();
    }


    private static void Copy(string srcpath,string targetpath)
    {
        try
        {
            if (!Directory.Exists(targetpath))
            {
                Directory.CreateDirectory(targetpath);
            }
            string srcdir = Path.Combine(targetpath,Path.GetFileName(srcpath));
            if (Directory.Exists(srcpath))
                srcdir += Path.DirectorySeparatorChar;

            if (!Directory.Exists(srcdir))
                Directory.CreateDirectory(srcdir);
            //获取所有文件名
            string[] files = Directory.GetFileSystemEntries(srcpath);

            foreach (string file in files)
            {
                //是否为文件夹
                if (Directory.Exists(file))
                {
                    Copy(file,srcdir);
                }
                //为文件
                else
                {
                    File.Copy(file,srcdir+Path.GetFileName(file),true);
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError("无法复制："+srcpath+" 到"+targetpath);
        }
    }


    public static void DeleteDir(string srcPath)
    {
        try
        {
            DirectoryInfo dirinfo = new DirectoryInfo(srcPath);
            FileSystemInfo[] fileinfo = dirinfo.GetFileSystemInfos();
            foreach (FileSystemInfo  info in fileinfo)
            {
                if (info is DirectoryInfo)
                {
                    DirectoryInfo subdir = new DirectoryInfo(info.FullName);
                    subdir.Delete(true);
                }
                else
                {
                    File.Delete(info.FullName);
                }
            }
        }
        catch (Exception e)
        {

            Debug.LogError("删除文件是吧 "+e);
        }
    }
}
